package ldkxingzhe.ldkxingzhedemo.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by liangdengke on 2015/9/11.
 */
public class StockQuoteActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = StockQuoteActivity.class.getSimpleName();
    private ToggleButton mToggleButton;
    private Button mButton;
    private IStockQuoteService mService = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        mToggleButton = new ToggleButton(this);
        mButton = new Button(this);
        mToggleButton.setText("check to bind service");
        mButton.setText("CLick to get number");
        layout.addView(mToggleButton);
        layout.addView(mButton);
        setContentView(layout);
        mToggleButton.setChecked(false);
        mButton.setEnabled(false);
        mToggleButton.setOnClickListener(this);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == mToggleButton){
            if(mToggleButton.isChecked()){
                String tmpName = IStockQuoteService.class.getName();
                LogDemo.v(TAG,"mToggleButton was clicked, checked is true,  and tmpName = " + tmpName);
                bindService(new Intent(tmpName),mServiceConnection, Context.BIND_AUTO_CREATE);
            }else{
                unbindService(mServiceConnection);
                mButton.setEnabled(false);
            }
        }else if(v == mButton){
            callService();
        }else{
            LogDemo.v(TAG,"Are you kidding me?");
        }
    }

    private void callService(){
        try {
            double result = mService.getQuote("Android");
            LogDemo.v(TAG, "result = " + result);
            mService.setPerson("Android",new Person("xingzhe","ldkxingzhe@163.com",22));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        LogDemo.v(TAG,"onDestroy");
        if(mButton.isEnabled()){
            unbindService(mServiceConnection);
        }
        super.onDestroy();
    }

    private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogDemo.v(TAG,"onServiceConnected");
            mService = IStockQuoteService.Stub.asInterface(service);
            mToggleButton.setChecked(true);
            mButton.setEnabled(true);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogDemo.v(TAG,"onServiceDisconnected");
            mToggleButton.setChecked(false);
            mButton.setEnabled(false);
        }
    };
}
